package com.lei.yohuo001java.one_fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.core.widget.NestedScrollView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.lei.mvp_retrofitlibrary.http.bean.GoodsBean;
import com.lei.mvp_retrofitlibrary.mvp.view.BaseFragment;
import com.lei.yohuo001java.Main2Activity;
import com.lei.yohuo001java.R;
import com.lei.yohuo001java.a_fragment.Fragment1;
import com.lei.yohuo001java.adapter.CommonAdaptera;
import com.lei.yohuo001java.adapter.MyAdapterViewPage;
import com.lei.yohuo001java.adapter.MyGoodsAdapter;
import com.lei.yohuo001java.contract.ShopContract;
import com.lei.yohuo001java.model.ShopModel;
import com.lei.yohuo001java.presenter.ShopPresenter;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class AFragment extends BaseFragment<ShopPresenter> implements ShopContract.View  {
    private NestedScrollView aFragNested;
    private Banner aFragBanner;
    private CommonTabLayout commonTabLayout;
    private RecyclerView fragOneRv1;
    private ArrayList<CustomTabEntity> tabEntitys = new ArrayList<>();
    private Banner aFragBanner2;
    private List<String> listbeans1 = new ArrayList<>();
    private List<String> listbeans2 = new ArrayList<>();
    private List<GoodsBean.DataBean> list = new ArrayList<>();
    private MyGoodsAdapter adapter;
    private int page = 1;

    @Override
    public int bindlayout() {
        return R.layout.fragment_a;
    }

    @Override
    public void onStart() {
        super.onStart();
        mPresenter.initShop(page);
    }

    @Override
    public void initView() {
        aFragNested = (NestedScrollView) findViewById(R.id.a_frag_nested);
        aFragBanner = (Banner) findViewById(R.id.a_frag_banner);
        commonTabLayout = (CommonTabLayout) findViewById(R.id.a_frag_sliding_tab_layout);
        fragOneRv1 = (RecyclerView) findViewById(R.id.a_frag_fl);
        aFragBanner2 = (Banner) findViewById(R.id.a_frag_banner2);
        mPresenter = new ShopPresenter(new ShopModel(),this);
    }

    @Override
    public void initData() {
        tabEntitys.add(new CommonAdaptera("推荐"));
        tabEntitys.add(new CommonAdaptera("球鞋"));
        tabEntitys.add(new CommonAdaptera("买家秀"));
        tabEntitys.add(new CommonAdaptera("特惠"));
        tabEntitys.add(new CommonAdaptera("男装"));
        tabEntitys.add(new CommonAdaptera("高街"));
        tabEntitys.add(new CommonAdaptera("女装"));
        tabEntitys.add(new CommonAdaptera("生活"));
        listbeans1.add("https://img.alicdn.com/i2/2207430758173/O1CN01a3PRUc2AFHdDbk0iR_!!2207430758173.jpg");
        listbeans1.add("https://img.alicdn.com/i1/2207430758173/O1CN016eVhuJ2AFHd1G10Oo_!!2207430758173.jpg");
        listbeans1.add("https://img.alicdn.com/i3/2207430758173/O1CN01oLdbmt2AFHdDBKSpz_!!2207430758173.jpg");

        listbeans2.add("https://img.alicdn.com/i2/28689/O1CN01rVS65P2E3bxwzmKIj_!!28689.jpg");
        listbeans2.add("https://img.alicdn.com/i4/28689/O1CN01UMC6v12E3by4iEt0t_!!28689.jpg");
        BannerLun(aFragBanner,listbeans1);
        BannerLun(aFragBanner2,listbeans2);
        commonTabLayout.setTabData(tabEntitys);

        adapter = new MyGoodsAdapter(R.layout.item_layout,list);
        fragOneRv1.setAdapter(adapter);
        fragOneRv1.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getActivity(), Main2Activity.class);
                intent.putExtra("banner_zy",list.get(position));
                startActivity(intent);
            }
        });
        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                list.clear();
                page = position+1;
                mPresenter.initShop(page);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

    }

    @Override
    public void initShop(GoodsBean goodsBean) {

        List<GoodsBean.DataBean> data = goodsBean.getData();
        list.addAll(data);
        adapter.notifyDataSetChanged();
    }

    public static void BannerLun(Banner banner,List<String> listbean){
        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                String list = (String) path;
                Glide.with(context).load(list).into(imageView);
            }
        });
        banner.setImages(listbean);
        banner.start();
    }

    @Override
    public void showToast(String msg) {

    }
}
